#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/videodev2.h>

int main(int argc, char* argv[])
{
    if(argc != 2){
        printf("app /dev/video0");
        return -1;
    }
    //打开摄像头
    int fd = open(argv[1], O_RDWR);
    if(fd < 0){
        perror("open failed");
        return -1;
    }

    struct v4l2_fmtdesc v4fmt;
    struct v4l2_capability cap;
    v4fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    int i = 0;
    while (1)
    {
        /* code */
        v4fmt.index = i;
        i++;
        int ret = ioctl(fd, VIDIOC_ENUM_FMT, &v4fmt);
        if(ret < 0){
            perror("获取格式失败");
            break;
        }
        printf("index = %d\n", v4fmt.index);
        printf("flags = %d\n", v4fmt.flags);
        printf("description = %s\n", v4fmt.description);
        unsigned char *p = (unsigned char*)&v4fmt.pixelformat;
        printf("pixelformat = %c%c%c%c\n", p[0], p[1], p[2], p[3]);
        printf("reserved = %d\n", v4fmt.reserved[0]);
    }
    
    int ret = ioctl(fd, VIDIOC_QUERYCAP, &cap);
    if(ret < 0)
        perror("获取功能失败");
    printf("drivers:%s\n", cap.driver);
    if(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)
        printf("%s 支持视频捕获\n", argv[1]);
    if(cap.capabilities & V4L2_CAP_STREAMING)
        printf("%s 支持流读写\n", argv[1]);

    close(fd);
    
    return 0;
}